﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class pro_mis_weight_editweight2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MasApp.BLL.Check.CheckLogin();
            if (Session["currentweightmissionid"] != null && Request.QueryString["ruleid"] != null)
            {
                int weightmissionid = Convert.ToInt32(Session["currentweightmissionid"]);
                HyperLink1.NavigateUrl = "editweight.aspx?weightmissionid=" + weightmissionid.ToString();
                int ruleid = Convert.ToInt32(Request.QueryString["ruleid"].ToString());

                MasApp.Model.appindicatororder aio = new MasApp.Model.appindicatororder();
                aio.Weightmissionid = weightmissionid;
                int indinum = MasApp.BLL.Search.SearchIndicatorNumber(ruleid);
                foreach (GridViewRow gr in GridView1.Rows)
                {
                    DropDownList ddl = (DropDownList)gr.FindControl("DropDownList1");
                    for (int io = 0; io < indinum; io++)
                    {
                        ddl.Items.Add(new ListItem((io + 1).ToString(), (io + 1).ToString()));
                    }
                    //显示当前排序
                    aio.Indicatorid = Convert.ToInt32(((Label)gr.FindControl("Label1")).Text);
                    aio.Indicatororder = MasApp.BLL.Search.SearchAppIndicatorOrder(aio);
                    ((Label)gr.FindControl("Label3")).Text = aio.Indicatororder.ToString();
                }
                //通过其中一个indicatorid 查找是否已经排序过
                if (MasApp.BLL.Search.ExistsAppIndicatorOrders(aio))
                {
                    HyperLink2.NavigateUrl = "compare2.aspx?ruleid=" + ruleid.ToString();
                    HyperLink2.Visible = true;
                }
            }
            else
            {
                MasApp.BLL.ResponseHelper.alert("非法操作","../mislist.aspx");
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        bool bo = true;
        for (int i1 = 0; i1 < GridView1.Rows.Count; i1++)
        {
            for (int i2 = i1 + 1; i2 < GridView1.Rows.Count; i2++)
            {
                if (((DropDownList)GridView1.Rows[i1].FindControl("DropDownList1")).SelectedValue == ((DropDownList)GridView1.Rows[i2].FindControl("DropDownList1")).SelectedValue)
                {
                    Label lb = new Label();
                    lb.Text = "<br /><fort color=red>请检查并修改重复的选项！</font>";
                    form1.Controls.Add(lb);
                    bo = false;
                    break;
                }
            }
            if (!bo)
            { break; }
        }
        if (bo)
        {
            int weightmissionid = int.Parse(Session["currentweightmissionid"].ToString());

            MasApp.Model.weight we = new MasApp.Model.weight();
            we.Elementtype = "指标";
            we.Weightmissionid = weightmissionid;
            we.Weightvalue = 0.0F;
            MasApp.Model.appindicatororder aio = new MasApp.Model.appindicatororder();
            aio.Weightmissionid = weightmissionid;
            //读出排序
            foreach (GridViewRow gr in GridView1.Rows)
            {
                DropDownList ddl = (DropDownList)gr.FindControl("DropDownList1");
                aio.Indicatororder = int.Parse(ddl.SelectedValue);
                Label lb = (Label)gr.FindControl("Label1");
                aio.Indicatorid = int.Parse(lb.Text);
                //如果已经存在则更新，否则插入
                if (MasApp.BLL.Search.ExistsAppIndicatorOrder(aio))
                {
                    MasApp.BLL.Update.UpdateAppIndicatorOrder(aio);
                }
                else
                {
                    MasApp.BLL.reg.InsertAppIndicatorOrder(aio);
                    we.Elementid = int.Parse(lb.Text);
                    MasApp.BLL.reg.RegWeight(we);
                }
            }

            MasApp.BLL.ResponseHelper.alert("修改成功", "editweight2.aspx?ruleid=" 
                + Request.QueryString["ruleid"].ToString());
        }
    }
}
